const http = require('../../util/http')
const QR = require('./weapp-qrcode.js')
const rpx2px = xhs.getSystemInfoSync().windowWidth / 750
Page({
  onShareAppMessage() {
    return {
      title: '首页',
      path: 'entry/index/index',
    };
  },
  data: {
    baseurl: http.baseURL,
    mode: 'aspectFill',
    goodsDetail:null,
    orderInfor:null,
    showModel:false,
    couponCode:'',
  },
  onLoad(e){
    console.log(e)
    this.getOrderInfo(e.outTradeNo)
    this.setData({
      couponCode:'',
      showModel:false
    })
  },
  openGoodsDeatil(){
    xhs.navigateTo({
      url: '/entry/productDetail/index?id='+this.data.goodsDetail.id
    });
  },
  closeModel(){
    this.setData({
      couponCode:'',
      showModel:false
    })
  },
  getOrderInfo(outTradeNo) {
    xhs.showLoading({
      title: '加载中',
    })
    http.get('/api/order/getOrderInfo', {outTradeNo:outTradeNo}).then(res => {
      console.log(res)
      xhs.hideLoading()
      if (res.code == 200) {
        this.setData({
          goodsDetail: res.data.product,
          orderInfor: res.data,
        });
      }
    });
  },
  openDetial(e){
    const item = e.currentTarget.dataset.item
    console.log(item)
    if(item.verifyStatus == '1'){
      this.setData({
        couponCode:item.couponCode,
        showModel:true
      })
      setTimeout(() => {
        console.log('this.data',this.data.couponCode)
        var imgData = QR.drawImg(this.data.couponCode, {
            typeNumber: 3,//码点大小 1-40，数字越大，码点越小，二维码会显得越密集
            errorCorrectLevel: 'H',//纠错等级 H等级最高(30%) 简单来说，就是二维码被覆盖了多少仍然能被识别出来 详见qrcode.js
            //size: parseInt(rpx2px)
        })
        this.setData({
            qrcodeURL: imgData
        })
    }, 200);
    }else{
      xhs.showToast({
        title: '已核销',
        icon: 'none'
      });
    }
  },
  copyCode(){
    xhs.setClipboardData({
        data: this.data.couponCode,
        success(res) {
          xhs.showToast({
                title: "复制成功"
            });
        },
        fail(res) {
          console.log(`setClipboardData调用失败`);
        },
      });
},
});
